package com.link.play.fit.ai.ui.guide

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.databinding.FgStopGoalBinding
import com.link.play.fit.ai.ui.guide.ac.GuidePbAc
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch

class StopGoalFg : BasePageFragment<FgStopGoalBinding>(FgStopGoalBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val userGuideState = (requireActivity() as GuidePbAc).viewModel.userGuideState
        lifecycleScope.launch {
            userGuideState.collectLatest {
                when (it.stopGoal) {
                    0 -> {
                        binding.ll1.isSelected = true
                        binding.ll2.isSelected = false
                        binding.ll3.isSelected = false
                        binding.ll4.isSelected = false
                        binding.ll5.isSelected = false
                    }

                    1 -> {
                        binding.ll1.isSelected = false
                        binding.ll2.isSelected = true
                        binding.ll3.isSelected = false
                        binding.ll4.isSelected = false
                        binding.ll5.isSelected = false
                    }

                    2 -> {
                        binding.ll1.isSelected = false
                        binding.ll2.isSelected = false
                        binding.ll3.isSelected = true
                        binding.ll4.isSelected = false
                        binding.ll5.isSelected = false
                    }

                    3 -> {
                        binding.ll1.isSelected = false
                        binding.ll2.isSelected = false
                        binding.ll3.isSelected = false
                        binding.ll4.isSelected = true
                        binding.ll5.isSelected = false
                    }

                    4 -> {
                        binding.ll1.isSelected = false
                        binding.ll2.isSelected = false
                        binding.ll3.isSelected = false
                        binding.ll4.isSelected = false
                        binding.ll5.isSelected = true
                    }
                }
            }
        }
        binding.ll1.setOnClickListener {
            userGuideState.update {
                it.copy(stopGoal = 0)
            }
        }
        binding.ll2.setOnClickListener {
            userGuideState.update {
                it.copy(stopGoal = 1)
            }
        }
        binding.ll3.setOnClickListener {
            userGuideState.update {
                it.copy(stopGoal = 2)
            }
        }
        binding.ll4.setOnClickListener {
            userGuideState.update {
                it.copy(stopGoal = 3)
            }
        }
        binding.ll5.setOnClickListener {
            userGuideState.update {
                it.copy(stopGoal = 4)
            }
        }
    }
}